
export function getGeocodeApi(address) {
  return new Promise((resolve) => {
    AMap.plugin('AMap.Geocoder', () => {
      const geocoder = new AMap.Geocoder({
        city: '全国'
      })
      
      geocoder.getLocation(address, (status, result) => {
        if (status === 'complete') {
          resolve({
            status: '1',
            geocodes: result.geocodes.map(item => ({
              ...item,
              adcode: item.adcode,
              location: item.location.getLng() + ',' + item.location.getLat()
            }))
          })
        } else {
          resolve({
            status: '0',
            info: '查询失败'
          })
        }
      })
    })
  })
}

export function getDistrictByAdcode(adcode) {
  return new Promise((resolve) => {
    AMap.plugin('AMap.DistrictSearch', () => {
      const district = new AMap.DistrictSearch({
        level: 'city',
        extensions: 'all'
      })
      
      district.search(adcode, (status, result) => {
        if (status === 'complete') {
          resolve(result.districtList[0])
        } else {
          resolve(null)
        }
      })
    })
  })
}
